package com.examsys.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.examsys.po.Admin;
import com.examsys.po.Paper;
import com.examsys.service.AdminService;
import com.examsys.service.PaperService;
import com.examsys.util.JsonUtils;

@WebServlet("/paper")
public class PaperController extends BaseController{

	private Logger log=Logger.getLogger(this.getClass());
	
	private AdminService adminService;
	
	private PaperService paperService;
	
	/**
	 * 初始化工作，这里主要是给业务逻辑层属性做初始化
	 */
	@Override
	public void init() throws ServletException {
		super.init();
		adminService=lookUp("adminService");
		paperService=lookUp("paperService");
	}
	
	/**
	 * 添加的方法
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void addSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Paper paper=new Paper();
		String paper_name = request.getParameter("paper_name");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		Integer paper_minute = Integer.valueOf(request.getParameter("paper_minute"));
		Integer total_score = Integer.valueOf(request.getParameter("total_score"));
		String post_date = request.getParameter("post_date");
		String show_score = request.getParameter("show_score");
		Integer qorder = Integer.valueOf(request.getParameter("qorder"));
		String status = request.getParameter("status");
		String remark = request.getParameter("remark");
		
		Admin admin=(Admin) request.getSession().getAttribute("ADMIN");
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		log.info(paper);
		try {
			String partten="yyyy-MM-dd HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(partten);
			paper.setAdmin(admin);
			paper.setPaper_name(paper_name);
			paper.setStart_time(sdf.parse(start_time));
			paper.setEnd_time(sdf.parse(end_time));
			paper.setPaper_minute(paper_minute);
			paper.setTotal_score(total_score);
			paper.setPost_date(sdf.parse(post_date));
			paper.setShow_score(sdf.parse(show_score));
			paper.setQorder(qorder);
			paper.setStatus(status);
			paper.setRemark(remark);
		
			boolean flag = paperService.add(paper);
			if(!flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 初始化修改
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void update(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id=Integer.valueOf(request.getParameter("id"));
		log.info(id);
		Paper paper=null;
		try {
			paper=paperService.get(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(paper));
	}
	
	/**
	 * 保存修改
	 * @param paper
	 * @param admin_id
	 * @throws IOException 
	 */
	public void updateSave(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Admin admin=(Admin) request.getSession().getAttribute("ADMIN");
		Integer id = Integer.valueOf(request.getParameter("id"));
		String paper_name = request.getParameter("paper_name");
		String start_time = request.getParameter("start_time");
		String end_time = request.getParameter("end_time");
		Integer paper_minute = Integer.valueOf(request.getParameter("paper_minute"));
		Integer total_score = Integer.valueOf(request.getParameter("total_score"));
		String post_date = request.getParameter("post_date");
		String show_score = request.getParameter("show_score");
		Integer qorder = Integer.valueOf(request.getParameter("qorder"));
		String status = request.getParameter("status");
		String remark = request.getParameter("remark");
		
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		
		log.info(id);
		
		try {
			Paper oldpaper=paperService.get(id);
		
			String partten="yyyy-MM-dd HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(partten);
			oldpaper.setPaper_name(paper_name);
			oldpaper.setStart_time(sdf.parse(start_time));
			oldpaper.setEnd_time(sdf.parse(end_time));
			oldpaper.setPaper_minute(paper_minute);
			oldpaper.setTotal_score(total_score);
			oldpaper.setPost_date(sdf.parse(post_date));
			oldpaper.setShow_score(sdf.parse(show_score));
			oldpaper.setQorder(qorder);
			oldpaper.setStatus(status);
			oldpaper.setRemark(remark);
			
			boolean flag = paperService.update(oldpaper);
			if(!flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 删除试卷
	 * @param id
	 * @throws IOException 
	 */
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		Integer id=Integer.valueOf(request.getParameter("id"));
		log.info(id);
		Map jsonDatas=new HashMap();//存放json数据的集合
		jsonDatas.put("status", 0);//默认状态为0，表示操作失败
		try {
			boolean flag = paperService.delete(id);
			if(flag){
				jsonDatas.put("status", 1);//设置状态为1，表示操作成功
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//最终给JsonUtils转换为json数据输出给浏览器
		response.getWriter().write(JsonUtils.objectToJson(jsonDatas));
	}
	
	/**
	 * 试卷管理页面
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		log.info("进入试卷管理页面");
		request.getRequestDispatcher("paper/Paper.jsp").forward(request, response);
	}
	
}





